<template>
	<view class="mainbgc mainpadding2" v-if="pageShow">
		<header-top :messageInfo="{typeFlag:true,message:'盘点库存',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content">
					<view class="nostrongtext" @click="surebtn">确定</view>
				</view>
			</view>
		</header-top>
		<!-- 内容 -->
		<view class="mainpadding ffffff margin_top" v-for="(item,index) in list" :key="item.id">
			<view class="flexbetween">
				<view class="flexleft">
					<view class="fangbtn">{{index+1}}</view>
					<view class="titletext">{{item.name}}</view>
				</view>
				<view class="huisetext" :style="{color:Number(item.total_stock)>0?'#2cd551' : '#ff0000'}">实际库存：{{item.total_stock}}</view>
			</view>
			<view class="flexleft margin_top" @click="shengchang(item,index)" v-if="item.producedate_status==1">
				<view class="huisetext">生产日期：</view>
				<input type="text" class="inputs xiabiank" placeholder="无生产日期" disabled="" v-model="item.produce_time">
			</view>
			<view class="biaoti margin_top">盘点库存：</view>
			<view class="flexbetween margin_top">
				<view class="flexleft">
					<view class="flexleft" v-show="item.biggoodsunit.id">
						<input type="text" class="input" v-model="item.biggoodsunit.big_unit_stock">
						<view class="biaoti" style="margin: 0 10rpx;">{{item.biggoodsunit.name}}</view>
					</view>
					<view class="flexleft" v-show="item.middlegoodsunit.id">
						<input type="text" class="input" v-model="item.middlegoodsunit.middle_unit_stock">
						<view class="biaoti" style="margin: 0 10rpx;">{{item.middlegoodsunit.name}}</view>
					</view>
					<view class="flexleft" v-show="item.smallgoodsunit.id">
						<input type="text" class="input" v-model="item.smallgoodsunit.small_unit_stock">
						<view class="biaoti" style="margin: 0 10rpx;">{{item.smallgoodsunit.name}}</view>
					</view>
				</view>
				<view class="shanchuimg" @click="deleteGoods(index)"></view>
			</view>
		</view>
		<u-datetime-picker :show="timeshow" mode="date" title="选择生产日期" v-model="value1" @confirm="timersure" @cancel="timeshow=false">
		</u-datetime-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				timeshow: false,
				value1: Number(new Date()),
				getmsg: {
					ids:"",
					pd_id:"",
					warehouse_id:"",
				},
				pageShow:false,
				list:[],
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.getmsg));
			this.getmsg = data
			this.init()
		},
		methods: {
			init() {
				let _this = this;
				let data = {
					ids: this.getmsg.ids.join(),
					warehouse_id:this.getmsg.warehouse_id,
				}
				httpRequest.request('/dhtapi/goods/chooseGoodsList', 'POST', data, false, false, true).then(res => {
					res.data.forEach(item => {
						this.$set(item, "produce_time", "") // 生产日期
						// 大单位
						this.$set(item.biggoodsunit, "big_unit_stock", "") // 数量
						// 中单位
						this.$set(item.middlegoodsunit, "middle_unit_stock", "") // 数量
						// 小单位
						this.$set(item.smallgoodsunit, "small_unit_stock", "") // 数量
					})
					this.list = JSON.parse(JSON.stringify(res.data))
					console.log(this.list,9999);
					this.pageShow = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			//生产日期
			shengchang(item, index) {
				this.selectindex = index
				this.timeshow = true
			},
			timersure(e) {
				console.log(this.list[this.selectindex], 888)
				this.list[this.selectindex].produce_time = httpRequest.returnDate(e.value)
				this.timeshow = false
			},
			surebtn() {
				let goods_data = []
				for(let i=0;i<this.list.length;i++){
					let item = this.list[i]
					if(item.producedate_status==1 && item.produce_time=="" && (item.smallgoodsunit.small_unit_stock!="" || item.middlegoodsunit.middle_unit_stock!="" || item.biggoodsunit.big_unit_stock!="")){
						httpRequest.toast("请选择"+item.name+"的生产日期!")
						return false
					}
					goods_data.push({
						goods_id:item.id,
						produce_time:item.produce_time,
						small_unit_stock:item.smallgoodsunit.small_unit_stock===""?-1:item.smallgoodsunit.small_unit_stock,
						middle_unit_stock:item.middlegoodsunit.middle_unit_stock===""?-1:item.middlegoodsunit.middle_unit_stock,
						big_unit_stock:item.biggoodsunit.big_unit_stock===""?-1:item.biggoodsunit.big_unit_stock,
						total_stock:item.total_stock
					})
				}
				httpRequest.request('/dhtapi/Warehouse/addPdGoods', 'POST', {
					warehouse_pd_id:this.getmsg.pd_id,
					goods_data
				}, false, false, true).then(res => {
					if(res.code == 1){
						uni.navigateBack({
							delta:2
						})
					}else{
						httpRequest.toast(res.msg)
					}
					
				})
			},
			// 删除
			deleteGoods(index){
				this.list.splice(index,1)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.xiabiank {
		padding-bottom: 10rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
	.inputs {
		width: 40%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}
	.neibianju {
		padding-bottom: 200rpx;
	}

	// 底部
	.queding {
		margin: auto;
		width: 260rpx;
		height: 62rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
		border-radius: 74rpx;
		border: 2rpx solid #0BBDCB;
	}

	.btoombox {
		position: fixed;
		width: 100%;
		padding: 30rpx 30rpx 70rpx;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
	}

	.shanchuimg {
		width: 26rpx;
		height: 28rpx;
		background-image: url('@/static/image/system/shanchuer.png');
		background-size: 100% 100%;
	}

	.fangbtn {
		margin-right: 10rpx;
		padding: 10rpx 16rpx;
		text-align: center;
		font-size: 24rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #0BBDCB;
		background: rgba(11, 189, 203, .1);
		border-radius: 10rpx;
	}

	.input {
		width: 144rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
		height: 50rpx;
		text-align: center;
		background: rgba(0, 0, 0, 0.05);
		border-radius: 6rpx;
	}

	.biaoti {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}
</style>
